package com.example.algorithm.algorithmService;

import com.example.algorithm.enums.MonthsEnum;
import com.example.algorithm.mapper.ControlPowerMonthMapper;
import com.example.algorithm.pojo.TimeInfo;
import com.example.algorithm.result.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ControlPowerMonthService {


    @Autowired
    ControlAlgorithmService controlAlgorithmService;

    @Autowired
    ControlPowerMonthMapper controlPowerMonthMapper;

    ControlPowerMonth controlPowerMonth = new ControlPowerMonth();

    public void getControlPowerMonth() {
        HashMap<Date, ControlPowerDay> resultDayMap = controlAlgorithmService.getResultDayMap();
        ArrayList<ControlPowerDay> januaryList = new ArrayList<>();
        ArrayList<ControlPowerDay> februaryList = new ArrayList<>();
        ArrayList<ControlPowerDay> marchList = new ArrayList<>();
        ArrayList<ControlPowerDay> aprilList = new ArrayList<>();
        ArrayList<ControlPowerDay> mayList = new ArrayList<>();
        ArrayList<ControlPowerDay> juneList = new ArrayList<>();
        ArrayList<ControlPowerDay> julyList = new ArrayList<>();
        ArrayList<ControlPowerDay> augustList = new ArrayList<>();
        ArrayList<ControlPowerDay> septemberList = new ArrayList<>();
        ArrayList<ControlPowerDay> octoberList = new ArrayList<>();
        ArrayList<ControlPowerDay> novemberList = new ArrayList<>();
        ArrayList<ControlPowerDay> decemberList = new ArrayList<>();
        for (Date calculationDate : resultDayMap.keySet()) {
            ControlPowerDay controlPowerDay = resultDayMap.get(calculationDate);
            TimeInfo timeInfo = null;
            Integer month = null;
            try {
                timeInfo = getTime(calculationDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (timeInfo != null) {
                month = timeInfo.getMonth();
            }
            if (month != null) {
                if (MonthsEnum.January.getValue() <= month && month < MonthsEnum.February.getValue()) {
                    januaryList.add(controlPowerDay);
                } else if (MonthsEnum.February.getValue() <= month && month < MonthsEnum.March.getValue()) {
                    februaryList.add(controlPowerDay);
                } else if (MonthsEnum.March.getValue() <= month && month < MonthsEnum.April.getValue()) {
                    marchList.add(controlPowerDay);
                } else if (MonthsEnum.April.getValue() <= month && month < MonthsEnum.May.getValue()) {
                    aprilList.add(controlPowerDay);
                } else if (MonthsEnum.May.getValue() <= month && month < MonthsEnum.June.getValue()) {
                    mayList.add(controlPowerDay);
                } else if (MonthsEnum.June.getValue() <= month && month < MonthsEnum.July.getValue()) {
                    juneList.add(controlPowerDay);
                } else if (MonthsEnum.July.getValue() <= month && month < MonthsEnum.August.getValue()) {
                    julyList.add(controlPowerDay);
                } else if (MonthsEnum.August.getValue() <= month && month < MonthsEnum.September.getValue()) {
                    augustList.add(controlPowerDay);
                } else if (MonthsEnum.September.getValue() <= month && month < MonthsEnum.October.getValue()) {
                    septemberList.add(controlPowerDay);
                } else if (MonthsEnum.October.getValue() <= month && month < MonthsEnum.November.getValue()) {
                    octoberList.add(controlPowerDay);
                } else if (MonthsEnum.November.getValue() <= month && month < MonthsEnum.December.getValue()) {
                    novemberList.add(controlPowerDay);
                } else if (MonthsEnum.December.getValue() <= month && month < MonthsEnum.January.getValue()) {
                    decemberList.add(controlPowerDay);
                }
            }
        }
        if (januaryList.size() != 0) {
            getJanuaryResult(januaryList);
        }
        if (februaryList.size() != 0) {
            getFebruaryResult(februaryList);
        }
        if (marchList.size() != 0) {
            getMarchResult(marchList);
        }
        if (aprilList.size() != 0) {
            getAprilResult(aprilList);
        }
        if (mayList.size() != 0) {
            getMayResult(mayList);
        }
        if (juneList.size() != 0) {
            getJuneResult(juneList);
        }
        if (julyList.size() != 0) {
            getJulyResult(julyList);
        }
        if (augustList.size() != 0) {
            getAugust(augustList);
        }
        if (septemberList.size() != 0) {
            getSeptemberResult(septemberList);
        }
        if (octoberList.size() != 0) {
            getOctober(octoberList);
        }
        if (novemberList.size() != 0) {
            getNovemberResult(novemberList);
        }
        if (decemberList.size() != 0) {
            getDecemberResult(decemberList);
        }
    }

    private void getDecemberResult(ArrayList<ControlPowerDay> decemberList) {
        getAvgResult(decemberList);
    }

    private void getNovemberResult(ArrayList<ControlPowerDay> novemberList) {
        getAvgResult(novemberList);
    }

    private void getOctober(ArrayList<ControlPowerDay> octoberList) {
        getAvgResult(octoberList);
    }

    private void getSeptemberResult(ArrayList<ControlPowerDay> septemberList) {
        getAvgResult(septemberList);
    }

    private void getAugust(ArrayList<ControlPowerDay> augustList) {
        getAvgResult(augustList);
    }

    private void getJulyResult(ArrayList<ControlPowerDay> julyList) {
        getAvgResult(julyList);
    }

    private void getJuneResult(ArrayList<ControlPowerDay> juneList) {
        getAvgResult(juneList);
    }

    private void getMayResult(ArrayList<ControlPowerDay> mayList) {
        getAvgResult(mayList);
    }

    private void getAprilResult(ArrayList<ControlPowerDay> aprilList) {
        getAvgResult(aprilList);
    }

    private void getMarchResult(ArrayList<ControlPowerDay> marchList) {
        getAvgResult(marchList);
    }

    private void getFebruaryResult(ArrayList<ControlPowerDay> februaryList) {
        getAvgResult(februaryList);
    }

    private void getJanuaryResult(ArrayList<ControlPowerDay> januaryList) {
        getAvgResult(januaryList);
    }

    private void getAvgResult(ArrayList<ControlPowerDay> list) {
        Double initiativeDiscoveryMarkMonth = getInitiativeDiscoveryMarkMonth(list);

        Double reformMarkMonth = getReformMarkMonth(list);

        Double questionReformMarkMonth = getQuestionReformMarkMonth(list);

        Double stockScoreMonth = getStockScoreMonth(initiativeDiscoveryMarkMonth, reformMarkMonth, questionReformMarkMonth);

        Double passiveDiscoveryMarkMonth = getPassiveDiscoveryMarkMonth(list);

        Double recrudesceMarkMonth = getRecrudesceMarkMonth(list);

        Double seriousProblemIgnoreMarkMonth = getSeriousProblemIgnoreMarkMonth(list);

        Double repeatScoreMonth = getRepeatScoreMonth(passiveDiscoveryMarkMonth, recrudesceMarkMonth, seriousProblemIgnoreMarkMonth);

        Double controlPowerScoreMonth = getControlPowerScoreMonth(stockScoreMonth, repeatScoreMonth);

        Double immediateChangeScoreMonth = getImmediateChangeScoreMonth(list);   //   '即知即改'

        Double reformGoodScoreMonth = getReformGoodScoreMonth(list);        //   '整改到位'

        Double activeDiscoveryScoreMonth = getActiveDiscoveryScoreMonth(list);   //   '主动发现'

        Double drawInferencesScoreMonth = getDrawInferencesScoreMonth(list);    //   '举一反三'

     //   Double surfaceControlScoreMonth = getSurfaceControlScoreMonth(list);    //   '面上掌控'

        Double sumFiveScoreMonth = getSumFiveScoreMonth(immediateChangeScoreMonth, reformGoodScoreMonth, activeDiscoveryScoreMonth, drawInferencesScoreMonth);

        List<Date> dateList = list.stream().map(ControlPowerDay::getComputeDateDay).collect(Collectors.toList());
        TimeInfo timeInfo = null;
        Integer year = 0;
        Integer month = 0;

        for (Date date : dateList) {
            try {
                timeInfo = getTime(date);
                year = timeInfo.getYear();
                month = timeInfo.getMonth();

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        controlPowerMonth.setComputeDateMonth((year + "年" + month + "月"));
        controlPowerMonth.setInitiativeDiscoveryMarkMonth(initiativeDiscoveryMarkMonth);
        controlPowerMonth.setReformMarkMonth(reformMarkMonth);
        controlPowerMonth.setQuestionReformMarkMonth(questionReformMarkMonth);
        controlPowerMonth.setStockScoreMonth(stockScoreMonth);
        controlPowerMonth.setPassiveDiscoveryMarkMonth(passiveDiscoveryMarkMonth);
        controlPowerMonth.setRecrudesceMarkMonth(recrudesceMarkMonth);
        controlPowerMonth.setSeriousProblemIgnoreMarkMonth(seriousProblemIgnoreMarkMonth);
        controlPowerMonth.setRepeatScoreMonth(repeatScoreMonth);
        controlPowerMonth.setControlPowerScoreMonth(controlPowerScoreMonth);
        controlPowerMonth.setImmediateChangeScoreMonth(immediateChangeScoreMonth);
        controlPowerMonth.setReformGoodScoreMonth(reformGoodScoreMonth);
        controlPowerMonth.setActiveDiscoveryScoreMonth(activeDiscoveryScoreMonth);
        controlPowerMonth.setDrawInferencesScoreMonth(drawInferencesScoreMonth);
        //controlPowerMonth.setSurfaceControlScoreMonth(surfaceControlScoreMonth);
        controlPowerMonth.setSumFiveScoreMonth(sumFiveScoreMonth);
        for (ControlPowerDay controlPowerDay : list) {
            try {
                Integer day= getTime(controlPowerDay.getComputeDateDay()).getDay();
                if (day==15){
                    controlPowerMonth.setMiddleStockScoreMonth(controlPowerDay.getStockScoreDay());
                    controlPowerMonth.setMiddleIncreaseScoreMonth(controlPowerDay.getRepeatScoreDay());
                    controlPowerMonth.setMiddleListScoreMonth(controlPowerDay.getControlPowerScoreDay());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        controlPowerMonthMapper.insert(controlPowerMonth);
    }


    private Double getSumFiveScoreMonth(Double immediateChangeScoreDay, Double reformGoodScoreDay, Double activeDiscoveryScoreDay, Double drawInferencesScoreDay) {

        return immediateChangeScoreDay + reformGoodScoreDay + activeDiscoveryScoreDay + drawInferencesScoreDay ;
    }

//    private Double getSurfaceControlScoreMonth(ArrayList<ControlPowerDay> list) {
//        double surfaceControlScoreMonth = 0d;
//        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getSurfaceControlScoreDay).average();
//        if (average.isPresent()) {
//            surfaceControlScoreMonth = average.getAsDouble();
//        }
//        return surfaceControlScoreMonth;
//    }

    private Double getDrawInferencesScoreMonth(ArrayList<ControlPowerDay> list) {
        double drawInferencesScoreMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getDrawInferencesScoreDay).average();
        if (average.isPresent()) {
            drawInferencesScoreMonth = average.getAsDouble();
        }
        return drawInferencesScoreMonth;

    }

    private Double getActiveDiscoveryScoreMonth(ArrayList<ControlPowerDay> list) {

        double activeDiscoveryScoreMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getActiveDiscoveryScoreDay).average();
        if (average.isPresent()) {
            activeDiscoveryScoreMonth = average.getAsDouble();
        }
        return activeDiscoveryScoreMonth;

    }

    private Double getReformGoodScoreMonth(ArrayList<ControlPowerDay> list) {
        double reformGoodScoreMonth = 0d;
        OptionalDouble average =list.stream().mapToDouble(ControlPowerDay::getReformGoodScoreDay).average();
        if (average.isPresent()) {
            reformGoodScoreMonth = average.getAsDouble();
        }
        return reformGoodScoreMonth;


    }

    private Double getImmediateChangeScoreMonth(ArrayList<ControlPowerDay> list) {
        double immediateChangeScoreMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getImmediateChangeScoreDay).average();
        if (average.isPresent()) {
            immediateChangeScoreMonth = average.getAsDouble();
        }
        return immediateChangeScoreMonth;

    }

    private Double getControlPowerScoreMonth(Double stockScoreMonth, Double repeatScoreMonth) {
        return stockScoreMonth + repeatScoreMonth;
    }

    private Double getRepeatScoreMonth(Double passiveDiscoveryMarkMonth, Double recrudesceMarkMonth, Double seriousProblemIgnoreMarkMonth) {
        return passiveDiscoveryMarkMonth + recrudesceMarkMonth + seriousProblemIgnoreMarkMonth;
    }

    private Double getSeriousProblemIgnoreMarkMonth(ArrayList<ControlPowerDay> list) {
        double seriousProblemIgnoreMarkMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getSeriousProblemIgnoreMarkDay).average();
        if (average.isPresent()) {
            seriousProblemIgnoreMarkMonth = average.getAsDouble();
        }
        return seriousProblemIgnoreMarkMonth;
    }

    private Double getRecrudesceMarkMonth(ArrayList<ControlPowerDay> list) {
        double recrudesceMarkMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getRecrudesceMarkDay).average();
        if (average.isPresent()) {
            recrudesceMarkMonth = average.getAsDouble();
        }
        return recrudesceMarkMonth;

    }

    private Double getPassiveDiscoveryMarkMonth(ArrayList<ControlPowerDay> list) {
        double passiveDiscoveryMarkMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getPassiveDiscoveryMarkDay).average();
        if (average.isPresent()) {
            passiveDiscoveryMarkMonth = average.getAsDouble();
        }
        return passiveDiscoveryMarkMonth;

    }

    private Double getStockScoreMonth(Double initiativeDiscoveryMarkMonth, Double reformMarkMonth, Double questionReformMarkMonth) {
        return initiativeDiscoveryMarkMonth + reformMarkMonth + questionReformMarkMonth;
    }

    private Double getQuestionReformMarkMonth(ArrayList<ControlPowerDay> list) {
        double questionReformMarkMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getQuestionReformMarkDay).average();
        if (average.isPresent()) {
            questionReformMarkMonth = average.getAsDouble();
        }
        return questionReformMarkMonth;
    }

    private Double getReformMarkMonth(ArrayList<ControlPowerDay> list) {
        double reformMarkMonth = 0d;
        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getReformMarkDay).average();
        if (average.isPresent()) {
            reformMarkMonth = average.getAsDouble();
        }
        return reformMarkMonth;
    }

    private Double getInitiativeDiscoveryMarkMonth(ArrayList<ControlPowerDay> list) {
        double initiativeDiscoveryMarkMonth = 0d;

        OptionalDouble average = list.stream().mapToDouble(ControlPowerDay::getInitiativeDiscoveryMarkDay).average();
        if (average.isPresent()) {
            initiativeDiscoveryMarkMonth = average.getAsDouble();
        }
        return initiativeDiscoveryMarkMonth;
    }


    public TimeInfo getTime(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dateStr = sdf.format(date);
        Integer year = Integer.parseInt(dateStr.substring(0, 4));
        Integer month = Integer.parseInt(dateStr.substring(5, 7));
        Integer day = Integer.parseInt(dateStr.substring(dateStr.length() -2,dateStr.length()));
        TimeInfo timeInfo = new TimeInfo();
        timeInfo.setYear(year);
        timeInfo.setMonth(month);
        timeInfo.setDay(day);
        return timeInfo;
    }
}
